package com.xhcl.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xhcl.common.annotation.Log;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.common.core.domain.Stree;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.common.utils.StringUtils;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.system.domain.TGgHsdw;
import com.xhcl.system.service.ITGgHsdwService;

/**
 * 核算单位Controller
 * 
 * @author xhcl
 * @date 2020-08-25
 */
@Controller
@RequestMapping("/system/hsdw")
public class TGgHsdwController extends BaseController
{
    private String prefix = "system/hsdw";

    @Autowired
    private ITGgHsdwService tGgHsdwService;

    @RequiresPermissions("system:hsdw:view")
    @GetMapping()
    public String hsdw()
    {
        return prefix + "/hsdw";
    }

    /**
     * 查询核算单位树列表
     */
    
    @RequiresPermissions("system:hsdw:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TGgHsdw tGgHsdw)
    {
    	startPage();
    	List<TGgHsdw> list = tGgHsdwService.selectTGgHsdwList(tGgHsdw);
    	return getDataTable(list);
    }

    /**
     * 导出核算单位列表
     */
    @RequiresPermissions("system:hsdw:export")
    @Log(title = "核算单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGgHsdw tGgHsdw)
    {
        List<TGgHsdw> list = tGgHsdwService.selectTGgHsdwList(tGgHsdw);
        ExcelUtil<TGgHsdw> util = new ExcelUtil<TGgHsdw>(TGgHsdw.class);
        return util.exportExcel(list, "hsdw");
    }

    /**
     * 新增核算单位
     */
    @GetMapping(value = { "/add/{dwbm}", "/add/" })
    public String add(@PathVariable(value = "dwbm", required = false) String dwbm, ModelMap mmap)
    {
        if (StringUtils.isNotNull(dwbm))
        {
            mmap.put("tGgHsdw", tGgHsdwService.selectTGgHsdwById(dwbm));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存核算单位
     */
    @RequiresPermissions("system:hsdw:add")
    @Log(title = "核算单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TGgHsdw tGgHsdw)
    {
        return toAjax(tGgHsdwService.insertTGgHsdw(tGgHsdw));
    }
    
    /**
     * 新增保存核算单位
     */
    @RequiresPermissions("system:hsdw:sync")
    @Log(title = "同步非税单位", businessType = BusinessType.INSERT)
    @PostMapping("/sync")
    @ResponseBody
    public AjaxResult sync()
    {   
    	Map map=new HashMap<>();
        return toAjax(tGgHsdwService.calludpdateHsdw(map));
    }

    /**
     * 修改核算单位
     */
    @GetMapping("/edit/{dwbm}")
    public String edit(@PathVariable("dwbm") String dwbm, ModelMap mmap)
    {
        TGgHsdw tGgHsdw = tGgHsdwService.selectTGgHsdwById(dwbm);
        mmap.put("tGgHsdw", tGgHsdw);
        return prefix + "/edit";
    }

    /**
     * 修改保存核算单位
     */
    @RequiresPermissions("system:hsdw:edit")
    @Log(title = "核算单位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TGgHsdw tGgHsdw)
    {
        return toAjax(tGgHsdwService.updateTGgHsdw(tGgHsdw));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:hsdw:remove")
    @Log(title = "核算单位", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{dwbm}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("dwbm") String dwbm)
    {
        return toAjax(tGgHsdwService.deleteTGgHsdwById(dwbm));
    }

    /**
     * 选择核算单位树
     */
    @GetMapping(value = { "/selectHsdwTree/{dwbm}", "/selectHsdwTree/" })
    public String selectHsdwTree(@PathVariable(value = "dwbm", required = false) String dwbm, ModelMap mmap)
    {
        if (StringUtils.isNotNull(dwbm))
        {
            mmap.put("tGgHsdw", tGgHsdwService.selectTGgHsdwById(dwbm));
        }
        return prefix + "/tree";
    }
    

    /**
     * 加载核算单位树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Stree> treeData()
    {
        List<Stree> ztrees = tGgHsdwService.selectTGgHsdwTree();
        return ztrees;
    }
}
